import 'package:flutter/material.dart';
import 'package:smart_shop/pages/cart/index.dart';
import 'package:smart_shop/pages/category/index.dart';
import 'package:smart_shop/pages/home/index.dart';
import 'package:smart_shop/pages/mine/index.dart';

class TabBarPage extends StatefulWidget {
  const TabBarPage({super.key});

  @override
  State<TabBarPage> createState() => _TabBarPageState();
}

class _TabBarPageState extends State<TabBarPage> {
  // 记录当前索引
  int currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // 核心区域
      body: SafeArea(
        // 根据 index 切换 显示的内容
        child: IndexedStack(
          // 显示的页面
          index: currentIndex,
          // 页面数组
          children: const [HomePage(), CategoryPage(), CartPage(), MinePage()],
        ),
      ),
      // 底部的 导航区域
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        items: barItemList(),
        currentIndex: currentIndex,
        // 选中文字颜色，未选中的颜色
        selectedItemColor: Colors.purple,
        unselectedItemColor: Colors.blue,
        // Tap 轻敲屏幕
        onTap: (index) {
          setState(() {
            currentIndex = index;
          });
        },
      ),
    );
  }

  List<BottomNavigationBarItem> barItemList() {
    // List
    List<BottomNavigationBarItem> items = [];

    // 添加
    items.add(
      BottomNavigationBarItem(
          icon: Image.asset(
            'tabbar/home.png',
            width: 32,
            height: 32,
          ),
          activeIcon: Image.asset(
            'tabbar/home-active.png',
            width: 32,
            height: 32,
          ),
          label: '首页'),
    );
    items.add(
      BottomNavigationBarItem(
          icon: Image.asset(
            'tabbar/cate.png',
            width: 32,
            height: 32,
          ),
          activeIcon: Image.asset(
            'tabbar/cate-active.png',
            width: 32,
            height: 32,
          ),
          label: '分类'),
    );
    items.add(
      BottomNavigationBarItem(
          icon: Image.asset(
            'tabbar/cart.png',
            width: 32,
            height: 32,
          ),
          activeIcon: Image.asset(
            'tabbar/cart-active.png',
            width: 32,
            height: 32,
          ),
          label: '购物车'),
    );
    items.add(
      BottomNavigationBarItem(
          icon: Image.asset(
            'tabbar/user.png',
            width: 32,
            height: 32,
          ),
          activeIcon: Image.asset(
            'tabbar/user-active.png',
            width: 32,
            height: 32,
          ),
          label: '我的'),
    );
    items.add(
      BottomNavigationBarItem(
          icon: Image.asset(
            'tabbar/special.webp',
            width: 32,
            height: 32,
          ),
          activeIcon: Image.asset(
            'tabbar/special.webp',
            width: 32,
            height: 32,
          ),
          label: 'ikun'),
    );

    // 返回添加的结果
    return items;
  }
}
